package com.example.common_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.common_library.R;
import com.example.common_library.utils.ScreenUtil;

/**
 * @desc item文本
 */
public class TextItemView extends LinearLayout {
    private View sll_root, v_view;
    private TextView tv_left, tv_content_left, tv_number, tv_new_people, tv_content_right;
    private int leftContentColor, rightContentColor;
    private int pointLeftMargin, pointRightMargin;
    private float rightTextSize;
    private String leftTitle, leftText, rightText;
    private boolean isViewShow, isLeftShow, isNewShow, isRightShow;

    public TextItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextItemView);
        leftTitle = typedArray.getString(R.styleable.TextItemView_leftTitle);
        isViewShow = typedArray.getBoolean(R.styleable.TextItemView_isViewShow, false);
        isLeftShow = typedArray.getBoolean(R.styleable.TextItemView_isLeftShow, false);
        isNewShow = typedArray.getBoolean(R.styleable.TextItemView_isNewShow, false);
        isRightShow = typedArray.getBoolean(R.styleable.TextItemView_isRightShow, true);
        pointLeftMargin = (int) typedArray.getDimension(R.styleable.TextItemView_viewLeft, 4);
        pointRightMargin = (int) typedArray.getDimension(R.styleable.TextItemView_viewRight, 13);
        leftText = typedArray.getString(R.styleable.TextItemView_leftContent);
        leftContentColor = typedArray.getColor(R.styleable.TextItemView_leftColor, Color.parseColor("#333333"));

        rightText = typedArray.getString(R.styleable.TextItemView_rightContent);
        rightTextSize = typedArray.getDimension(R.styleable.TextItemView_rightSize, 14);
//        rightTextSize = ScreenUtil.sp2px(textSize);
        rightTextSize = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, rightTextSize, context.getResources().getDisplayMetrics());
        rightContentColor = typedArray.getColor(R.styleable.TextItemView_rightColor, Color.parseColor("#333333"));
        typedArray.recycle();

        initView(context);
        initData();
    }


    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_text_item, this, true);
        sll_root = findViewById(R.id.sll_root);
        tv_left = findViewById(R.id.tv_title);
        v_view = findViewById(R.id.v_view);
        tv_content_left = findViewById(R.id.tv_content_left);
        tv_number = findViewById(R.id.tv_number);
        tv_new_people = findViewById(R.id.tv_new_people);
        tv_content_right = findViewById(R.id.tv_content_right);
    }

    private void initData() {
        if (!TextUtils.isEmpty(leftTitle)) {
            tv_left.setText(leftTitle);
        }
        if (!TextUtils.isEmpty(rightText)) {
            tv_content_right.setText(rightText);
        }
        if (!TextUtils.isEmpty(leftText)) {
            tv_content_left.setText(leftText);
        }
        v_view.setVisibility(isViewShow ? VISIBLE : GONE);
        if (isViewShow) {
            LinearLayout.LayoutParams layoutParams = (LayoutParams) v_view.getLayoutParams();
            layoutParams.leftMargin = pointLeftMargin;
            layoutParams.rightMargin = pointRightMargin;
            v_view.setLayoutParams(layoutParams);
        }
        tv_content_left.setVisibility(isLeftShow ? VISIBLE : GONE);
        tv_new_people.setVisibility(isNewShow ? VISIBLE : GONE);
        tv_content_right.setVisibility(isRightShow ? VISIBLE : GONE);
        tv_content_left.setTextColor(leftContentColor);
        tv_content_right.setTextColor(rightContentColor);
        tv_content_right.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);

//        sll_root.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (itemClick != null) {
//                    itemClick.click();
//                }
//            }
//        });

    }

    public void setLeftTitle(String text) {
        this.leftTitle = text;
        tv_left.setText(leftTitle);
    }

    public void setLeftContent(String text) {
        this.leftText = text;
        this.isLeftShow = !TextUtils.isEmpty(text);
        tv_content_left.setText(leftText);
        tv_content_left.setVisibility(isLeftShow ? VISIBLE : GONE);
    }

    public void setLeftContentColor(int textColor) {
        this.leftContentColor = textColor;
        tv_content_left.setTextColor(getContext().getResources().getColor(leftContentColor));
    }

    public void setLeftNumber(String text) {
        tv_number.setText(text);
        tv_number.setVisibility(VISIBLE);
    }

    public void setIsNewPeople(boolean isNew) {
        this.isNewShow = isNew;
        tv_new_people.setVisibility(isNewShow ? VISIBLE : GONE);
    }

    public void setPointVisible(boolean isVisible) {
        this.isViewShow = isVisible;
        v_view.setVisibility(isViewShow ? VISIBLE : GONE);
    }

    public void setLeftNewVisible(boolean isVisible) {
        this.isNewShow = isVisible;
        tv_new_people.setVisibility(isNewShow ? VISIBLE : GONE);
    }

    public void setRightContent(String text) {
        this.rightText = text;
        this.isRightShow = !TextUtils.isEmpty(text);
        tv_content_right.setText(rightText);
        tv_content_right.setVisibility(isRightShow ? VISIBLE : GONE);
    }

    public void setRightContentColor(int textColor) {
        this.rightContentColor = textColor;
        tv_content_right.setTextColor(getContext().getResources().getColor(rightContentColor));
    }

    public interface ItemClick {
        void click();
    }

    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }
}
